import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../ui/theme/xlcolors.dart';

/// 手机号输入框组件
class ClearTextField extends StatefulWidget {
  const ClearTextField(
      {Key? key,
      this.onTextChangeed,
      this.controller,
      this.inputFormatters,
      this.focusNode,
      this.hintText,
      this.autofocus = false,
      this.bgColor = XlColors.bgInputLogin,
      this.obscureText = false})
      : super(key: key);
  final Function(String textChanged)? onTextChangeed;
  final TextEditingController? controller;
  final List<TextInputFormatter>? inputFormatters;
  final FocusNode? focusNode;
  final String? hintText;
  final bool autofocus;
  final Color bgColor;
  final bool obscureText;

  @override
  _ClearTextFieldState createState() => _ClearTextFieldState();
}

class _ClearTextFieldState extends State<ClearTextField> {
  var position = 0;
  late final FocusNode _focusNode;
  var showDelete = false;

  @override
  void initState() {
    super.initState();
    //恢复默认
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: SystemUiOverlay.values);
    _focusNode = widget.focusNode ?? FocusNode();
    _focusNode.addListener(() {
      setState(() {
        showDelete =
            _focusNode.hasFocus && (widget.controller?.text.length ?? 0) > 0;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints(),
      child: TextField(
        controller: widget.controller,
        maxLines: 1,
        onChanged: (value) {
          widget.onTextChangeed!(value);
          setState(() {
            showDelete = value.length > 0;
          });
        },
        keyboardType: TextInputType.number,
        obscureText: widget.obscureText,
        focusNode: _focusNode,
        textAlignVertical: TextAlignVertical.center,
        cursorColor: XlColors.colorCursor,
        // cursorHeight: double.infinity,
        autofocus: widget.autofocus,
        style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
        inputFormatters: widget.inputFormatters,
        decoration: InputDecoration(
            contentPadding: const EdgeInsets.fromLTRB(16, 0, 0, 0),
            hintText: widget.hintText,
            hintStyle: const TextStyle(
                color: Color(0xFFB0C5D7),
                fontSize: 16,
                height: 1.0,
                fontWeight: FontWeight.w400),
            border: const OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(8)),
                borderSide: BorderSide.none),
            filled: true,
            fillColor: widget.bgColor,
            suffixIcon: Visibility(
              child: IconButton(
                icon: Icon(
                  Icons.close,
                  color: Color(0xffbfbfbf),
                ),
                onPressed: () {
                  if (widget.controller != null) {
                    widget.controller?.clear();
                    widget.onTextChangeed!("");
                    setState(() {
                      showDelete = false;
                    });
                  }
                },
              ),
              visible: showDelete,
            )),
      ),
    );
  }

  String getTextLength() {
    return widget.controller?.text ?? "";
  }
}
